package com.zhuiyi.yiqingbackend.controller;

import com.zhuiyi.yiqingbackend.common.BaseResponse;
import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.common.ResultUtils;
import com.zhuiyi.yiqingbackend.domain.Notice;
import com.zhuiyi.yiqingbackend.domain.response.NoticeRespose;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/notice")
@Api(value = "公告接口", tags = "公告接口")
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
public class NoticeController {
    @Resource
    private NoticeService noticeService;
    /**
     * 获取公告列表
     * @return
     */
    @GetMapping("/getNotices")
    @ApiOperation(value = "获取公告列表", notes = "获取公告列表")
    public BaseResponse<List<Notice>> getNotices(){
        List<Notice> notices = noticeService.getNotices();
        return ResultUtils.success(notices);
    }
    /**
     * 获取公告列表 分等级
     */
    @GetMapping("/getNotices2")
    @ApiOperation(value = "获取公告列表 分等级", notes = "获取公告列表 分等级")
    public BaseResponse<NoticeRespose> getNotices2(){
        NoticeRespose noticeRespose = noticeService.getNotices2();
        return ResultUtils.success(noticeRespose);
    }

    /**
     * 添加公告
     * @param notice
     * @return
     */
    @PostMapping("/addNotice")
    @ApiOperation(value = "添加公告", notes = "添加公告")
    public BaseResponse<Boolean> addNotice(@RequestBody Notice notice){
        boolean ok = noticeService.addNotice(notice);
        if(!ok){
            throw new BusinessException(ErrorCode.FAIL, "添加失败");
        }
        return ResultUtils.success(ok);
    }

    /**
     * 删除公告
     * @param id
     * @return
     */
    @GetMapping("/deleteNotice/{id}")
    @ApiOperation(value = "删除公告", notes = "删除公告")
    public BaseResponse deleteNotice(@PathVariable int id){
        return ResultUtils.success(noticeService.deleteNotice(id));
    }

    /**
     * 更新公告
     * @param notice
     * @return
     */
    @PostMapping("/updateNotice")
    @ApiOperation(value = "更新公告", notes = "更新公告")
    public BaseResponse updateNotice(@RequestBody Notice notice){
        boolean b = noticeService.updateNotice(notice);
        if(!b){
            throw new BusinessException(ErrorCode.FAIL, "更新失败");
        }
        return ResultUtils.success(b);
    }

    /**
     * 根据id获取公告
     * @param id
     * @return
     */
    @GetMapping("/getNoticeById/{id}")
    @ApiOperation(value = "根据id获取公告", notes = "根据id获取公告")
    public BaseResponse<List<Notice>> getNoticeById(@PathVariable int id){
        return ResultUtils.success(noticeService.getNoticeById(id));
    }


}
